#include <bits/stdc++.h>
#include <string>
using namespace std;

const int size = 10;
int arr[size][size];
int root[size][size];

int main()
{

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			arr[i][j] = 10001;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> arr[i][j];
		}
	}
	// 循环列，行
	for (int i = 2; i <= m; i++) // 列
	{
		for (int j = 1; j <= n; j++) // 行
		{
			// 枚举当前坐标的上一个位子（3个方向 左上，左，左下)
			int leftTop = 0, left = 0, leftDown = 0;
			if (arr[j - 1][i - 1] < arr[j][i])
			{
				if (i == 2 || root[j - 1][i - 1])
					leftTop = root[j - 1][i - 1] + 1;
			}
			if (arr[j][i - 1] < arr[j][i])
			{
				if (i == 2 || root[j][i - 1])
				{
					left = root[j][i - 1] + 1;
				}
			}
			if (arr[j + 1][i - 1] < arr[j][i])
			{
				if (i == 2 || root[j + 1][i - 1])
				{
					leftDown = root[j + 1][i - 1] + 1;
				}
			}
			root[j][i] = max(leftTop, max(left, leftDown));
		}
	}
	int maxx = 0;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			maxx = max(maxx, root[i][j]);
		}
	}
	cout << maxx;

	return 0;
}